// pages/login/login.js
const app = getApp();
const $api = require('../../request/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    // 验证码图片
    verifyCode_prc: "",
    username: "",
    userpassword: "",
    verifyCode: ""
  },

  // 实现登陆
  toLogin () {
    let data = { 
      username: this.data.username, 
      userpassword: this.data.userpassword, 
      verifyCode: this.data.verifyCode 
    };
    $api.login(data).then(res => {
      console.log(res);
    });
  },

  // 切换验证码图片
  changeVerifyCode () {
      this.setData({
        verifyCode_prc: this.timestamp("http://localhost:9000/user/verifyCode")
      })
  },

  onLoad () {
    // 页面加载时顺便加载验证码图片
    this.setData({
      //verifyCode_prc: this.timestamp("http://localhost:9000/user/verifyCode")
    })
  },

   //为url添加时间戳
   timestamp (url) {
      var getTimestamp = new Date().getTime();
      if (url.indexOf("?") > -1) {
          url = url + "&timestamp=" + getTimestamp
      } else {
          url = url + "?timestamp=" + getTimestamp
      }
      return url;
   }
})